﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tzx.Tools.Payroll2Images.Exe.Api;
using Tzx.Utilities;

namespace Tzx.Tools.Payroll2Images.Exe.FilesSender
{
    /// <summary>
    /// 将命名文件调用API发送
    /// </summary>
    public partial class UcSender : UserControl
    {
        #region 初始化

        /// <summary>
        /// 构造
        /// </summary>
        public UcSender()
        {
            InitializeComponent();

            //初始设置列表控件
            InitListView();

            LblReadText.Text = READ_TEXT;
        }

        /// <summary>
        /// 
        /// </summary>
        private const string READ_TEXT = @"文件名中需包含接收人“中文名/英文名/工号”至少一项，如文件名“李大明Tom00365.jpg”";

        /// <summary>
        /// 
        /// </summary>
        private string Title
        {
            get
            {
                if (ParentForm == null)
                {
                    return String.Empty;
                }
                return ParentForm.Text;
            }
        }

        private const string SubItem_文件路径地址全名 = "文件路径地址全名 ";
        private const string SubItem_文件短名 = "文件短名";
        private const string SubItem_提交 = "提交";
        private const string SubItem_发送状态 = "发送状态";
        private const string SubItem_阅读状态 = "阅读状态";
        private const string SubItem_撤回 = "撤回";
        private const string SubItem_备注 = "备注";
        private const string SubItem_会话SessionId = "session_id";

        /// <summary>
        /// 初始设置ListView控件
        /// </summary>
        private void InitListView()
        {
            // 初始化ListView
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_文件路径地址全名, Width = 120 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_文件短名, TextAlign = HorizontalAlignment.Left, Width = 200 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_提交, TextAlign = HorizontalAlignment.Center, Width = 70 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_发送状态, TextAlign = HorizontalAlignment.Center, Width = 70 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_阅读状态, TextAlign = HorizontalAlignment.Center, Width = 70 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_撤回, TextAlign = HorizontalAlignment.Center, Width = 70 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_备注, TextAlign = HorizontalAlignment.Left, Width = 140 });
            this.LvFiless.Columns.Add(new ColumnHeader() { Text = SubItem_会话SessionId, TextAlign = HorizontalAlignment.Left, Width = 90 });

            //
            this.LvFiless.CheckBoxes = true;
            this.LvFiless.FullRowSelect = true;
            this.LvFiless.GridLines = true;           
            this.LvFiless.MultiSelect = false;            
            this.LvFiless.View = System.Windows.Forms.View.Details;
        }

        #endregion

        #region public

        /// <summary>
        /// 外部赋值
        /// </summary>
        /// <param name="directory"></param>
        public void SendDirectory(string directory)
        {
            TxtFilesInFolder.Text = string.Format("{0}", directory);
            FillFileSs(directory);//加载到列表
        }

        #endregion

        #region 事件方法_按钮(文件夹选择)

        /// <summary>
        /// 1.选择文件夹
        /// </summary>
        private void BtnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件夹路径";
            dialog.ShowNewFolderButton = true;

            //初始位置
            string dir = string.Format("{0}", TxtFilesInFolder.Text).Trim();
            if (dir != string.Empty)
            {
                if (Directory.Exists(dir))
                {
                    dialog.SelectedPath = dir;
                }
            }
            else
            {
                //当前位置
                dialog.SelectedPath = Application.StartupPath;
            }

            //
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string path = dialog.SelectedPath;
                TxtFilesInFolder.Text = path;

                FillFileSs(path);//加载到列表
            }
        }

        /// <summary>
        /// 2.加载到列表
        /// </summary>
        private void BtnFill_Click(object sender, EventArgs e)
        {
            string dir = TxtFilesInFolder.Text;
            if (!Directory.Exists(dir))
            {
                MessageBox.Show("文件夹不存在", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            FillFileSs(dir);//加载到列表
        }

        /// <summary>
        /// 3.打开文件夹
        /// </summary>
        private void BtnOpenFolder_Click(object sender, EventArgs e)
        {
            string dir = TxtFilesInFolder.Text;
            if (!Directory.Exists(dir))
            {
                MessageBox.Show("文件夹不存在", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Diagnostics.Process.Start(dir);
        }

        ///// <summary>
        ///// 4.移除所选
        ///// </summary>
        //private void BtnRemove_Click(object sender, EventArgs e)
        //{
        //    string text = "确定要将已勾选文件从任务列表中移除？\n\n(不是删除文件)";
        //    if (MessageBox.Show(text, "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        //    {
        //        return;
        //    }

        //    int count = LvFiless.Items.Count;
        //    for (int idx = count - 1; idx >= 0; idx--)
        //    {
        //        if (LvFiless.Items[idx].Checked)
        //        {
        //            LvFiless.Items.RemoveAt(idx);
        //        }
        //    }
        //}

        /// <summary>
        /// 4.移除所选
        /// </summary>
        private void LnkRemove_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string text = "确定要将已勾选文件从任务列表中移除？\n\n(不是删除文件)";
            if (MessageBox.Show(text, "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            int count = LvFiless.Items.Count;
            for (int idx = count - 1; idx >= 0; idx--)
            {
                if (LvFiless.Items[idx].Checked)
                {
                    LvFiless.Items.RemoveAt(idx);
                }
            }
        }

        #endregion

        #region 事件方法_按钮(发送,获取状态)

        /// <summary>
        /// 0.全部勾选
        /// </summary>
        private void CbCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = CbCheckAll.Checked;
            foreach (ListViewItem lvi in LvFiless.Items)
            {
                lvi.Checked = isChecked;
            }
        }

        /// <summary>
        /// 1.提交发送
        /// </summary>
        private void BtnBeginSend_Click(object sender, EventArgs e)
        {
            int count_checked = GetCheckedItems().Count;
            if (count_checked == 0)
            {
                MessageBox.Show("没有勾选任何文件", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //
            string text = "开始将已勾选的" + count_checked.ToString() + "个文件提交发送？";
            if (MessageBox.Show(text, "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            //开始
            panel4Bottom.Enabled = false;
            LvFiless.Enabled = false;
            Cursor = Cursors.WaitCursor;

            try
            {
                Do_Submit_Checked();//提交发送按钮
            }
            finally
            {
                //结束
                panel4Bottom.Enabled = true;
                LvFiless.Enabled = true;
                Cursor = Cursors.Default;

                progressBar1.Value = 0;//重置
            }

            //TODO:刷新一次发送进度状态()
        }

        /// <summary>
        /// 2.刷新发送进度
        /// </summary>
        private void BtnRefreshProgress_Click(object sender, EventArgs e)
        {
            //开始
            panel4Bottom.Enabled = false;
            LvFiless.Enabled = false;
            Cursor = Cursors.WaitCursor;

            try
            {
                Do_RenewSendStatus_Checked(false);//刷新发送进度按钮
            }
            finally
            {
                //结束
                panel4Bottom.Enabled = true;
                LvFiless.Enabled = true;
                Cursor = Cursors.Default;

                progressBar1.Value = 0;//重置
            }
        }

        /// <summary>
        /// 3.刷新结果状态(阅读状态)
        /// </summary>
        private void BtnRefreshStatus_Click(object sender, EventArgs e)
        {
            //开始
            panel4Bottom.Enabled = false;
            LvFiless.Enabled = false;
            Cursor = Cursors.WaitCursor;

            try
            {
                Do_RenewReadStatus_Checked();//刷新结果阅读按钮
            }
            finally
            {
                //结束
                panel4Bottom.Enabled = true;
                LvFiless.Enabled = true;
                Cursor = Cursors.Default;

                progressBar1.Value = 0;//重置
            }
        }

        /// <summary>
        /// 4.撤回
        /// </summary>
        private void BtnRecall_Click(object sender, EventArgs e)
        {
            int count_checked = GetCheckedItems().Count;
            if (count_checked == 0)
            {
                MessageBox.Show("没有勾选任何文件", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //
            string text = "确定要将已勾选的" + count_checked.ToString() + "已提交发送成功的消息撤回？\n\n(仅能撤回24小时以内的消息)";
            if (MessageBox.Show(text, "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            //开始
            panel4Bottom.Enabled = false;
            LvFiless.Enabled = false;
            Cursor = Cursors.WaitCursor;
            
            try
            {
                Do_Recall_Checked();//撤回按钮
            }
            finally
            {
                //结束
                panel4Bottom.Enabled = true;
                LvFiless.Enabled = true;
                Cursor = Cursors.Default;

                progressBar1.Value = 0;//重置
            }
        }

        #endregion

        #region 事件方法_右键菜单

        /// <summary>
        /// 复制全部
        /// </summary>
        private void MnuCopyAll_Click(object sender, EventArgs e)
        {
            string fullText = string.Empty;
            for (int itemIdx = 0; itemIdx < LvFiless.Items.Count; itemIdx++)
            {
                string singleLine = string.Empty;

                //标题行
                if (itemIdx == 0)
                {
                    for (int colIdx = 0; colIdx < LvFiless.Columns.Count; colIdx++)
                    {
                        singleLine = singleLine + LvFiless.Columns[colIdx].Text + "\t";
                    }
                    singleLine = singleLine.Substring(0, singleLine.Length - 1) + "\r\n";
                }

                //内容行
                for (int colIdx = 0; colIdx < LvFiless.Columns.Count; colIdx++)
                {
                    singleLine = singleLine + LvFiless.Items[itemIdx].SubItems[colIdx].Text.Replace("\n", "") + "\t";
                }//for

                fullText = fullText + singleLine.Substring(0, singleLine.Length - 1) + "\r\n";
            }//for

            //
            Clipboard.SetText(fullText);
            MessageBox.Show("标题及全部行已复制到剪切板", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 复制选中行
        /// </summary>
        private void MnuCopyRow_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            List<int> rowIndex = new List<int>();
            foreach (ListViewItem item in LvFiless.SelectedItems)
            {
                string singleLine = string.Empty;

                //内容行
                for (int colIdx = 0; colIdx < LvFiless.Columns.Count; colIdx++)
                {
                    singleLine = singleLine + item.SubItems[colIdx].Text.Replace("\n", "") + "\t";
                }
                builder.AppendLine(singleLine.Substring(0, singleLine.Length - 1));
                rowIndex.Add(item.Index);
            }//foreach

            //
            Clipboard.SetText(builder.ToString());
            MessageBox.Show(
                string.Format("选中{0}行已复制到剪切板",
                    rowIndex.Count == 1 ? " 第" + (rowIndex[0] + 1) + "" : "共有" + rowIndex.Count + ""),
                Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        #region private辅助方法

        /// <summary>
        /// 文件夹里的文件填充到ListView控件
        /// </summary>
        /// <param name="folder"></param>
        private void FillFileSs(string folder)
        {
            LvFiless.Items.Clear();//清空

            //
            string[] filess = Directory.GetFiles(folder, "*");
            List<ListViewItem> list = GetItemList(filess); //转换成列表控件          
            foreach (ListViewItem li in list)
            {
                LvFiless.Items.Add(li);
            }
        }

        /// <summary>
        /// 文件列表转换成ListView控件的Item
        /// </summary>
        /// <param name="filess"></param>
        /// <returns></returns>
        private List<ListViewItem> GetItemList(IEnumerable<string> filess)
        {
            int folder_path_len = TxtFilesInFolder.Text.Length; //文件夹
            List<ListViewItem> list = new List<ListViewItem>();
            foreach (string file in filess)
            {
                string file_full_name = file;
                if (
                    file_full_name.EndsWith(".jpg") ||
                    file_full_name.EndsWith(".jpeg") ||
                    file_full_name.EndsWith(".png") ||
                    file_full_name.EndsWith(".gif") ||
                    file_full_name.EndsWith(".bmp")
                    )
                {
                    //
                    string short_name = file_full_name.Substring(folder_path_len + 1);

                    ListViewItem item = new ListViewItem();
                    item.Text = file_full_name;  //文件路径全名
                    item.Checked = true;    //默认选中
                    //item.Tag = new ApiStatus(short_name);//状态数据
                    //item.Tag = new ApiStatus(file_full_name);//状态数据
                    item.Tag = new ApiStatus() { file_full_path_name = file_full_name };//状态数据
                    list.Add(item);

                    //item.UseItemStyleForSubItems = false;//单独设置样式ListViewSubItem

                    //0,原Text

                    //1,                    
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = short_name, Name = SubItem_文件短名 });
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = "", Name = SubItem_提交 });
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = "", Name = SubItem_发送状态 });
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = "", Name = SubItem_阅读状态 });
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = "", Name = SubItem_撤回 });
                    item.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = "", Name = SubItem_备注 });
                }
            }//foreach
            return list;
        }

        /// <summary>
        /// 勾选列表
        /// </summary>
        private List<ListViewItem> GetCheckedItems()
        {
            List<ListViewItem> list = new List<ListViewItem>();
            foreach (ListViewItem lvi in LvFiless.Items)
            {
                if (lvi.Checked)
                {
                    list.Add(lvi);
                }
            }
            return list;
        }

        /// <summary>
        /// 文件夹名称
        /// </summary>
        private string sub_folder = "已提交_" + DateTime.Now.ToString("yyyyMMddHHmmss");

        /// <summary>
        /// 移动已提交文件
        /// </summary>
        /// <param name="file"></param>
        private void MoveSendedFile(string file)
        {
            //
            if (!File.Exists(file))
            {
                LogFile.Instance().LogText("文件不存在", file);
                return;
            }

            string only_file = string.Format("{0}", Path.GetFileName(file));
            string only_path = string.Format("{0}", Path.GetDirectoryName(file));

            //
            string dir = Path.Combine(only_path, sub_folder);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string file2 = Path.Combine(dir, only_file);
            File.Move(file, file2);
        }

        #endregion

        #region 遍历列表Item调用API

        /// <summary>
        /// 显示各状态
        /// </summary>
        /// <param name="lvi"></param>
        /// <param name="status"></param>
        private void ShowStatus(ListViewItem lvi, ApiStatus status)
        {
            //设置SubItems颜色会闪烁

            lvi.SubItems[SubItem_提交].Text = status.status_submit_send == YesOrNo.Default
                ? string.Empty
                : status.status_submit_send.ToString();

            lvi.SubItems[SubItem_发送状态].Text = status.status_progress == YesOrNo.Default
                ? string.Empty
                : status.status_progress.ToString();

            lvi.SubItems[SubItem_阅读状态].Text = status.status_result_read == YesOrNo.Default
                ? string.Empty
                : status.status_result_read.ToString();

            lvi.SubItems[SubItem_撤回].Text = status.status_recall == YesOrNo.Default
                ? string.Empty
                : status.status_recall.ToString();

            lvi.SubItems[SubItem_备注].Text = status.remark_text;
        }

        /// <summary>
        /// 1.提交选中的发送
        /// </summary>
        private void Do_Submit_Checked()
        {
            List<ListViewItem> list = GetCheckedItems();
            int p_count = list.Count; //总数
            int p_index = 0; //第几个
            //progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条
            foreach (ListViewItem lvi in list)
            {
                //进度
                progressBar1.Value = (int) (((double) (p_index++)/(double) p_count)*100.00d); //进度条
                lblProgress.Text = String.Format("{0}/{1}", p_index, p_count); //进度

                //滚动选中
                int idx = lvi.Index;
                lvi.Selected = true; //选中行,灰色
                LvFiless.EnsureVisible(idx); //确保当前选定项是可见的,必要时使控件内容滚动

                //
                string file = lvi.Text;
                ApiStatus status = lvi.Tag as ApiStatus;
                if (status == null)
                {
                    string msg = "第" + (idx + 1).ToString() + "个内置Tag数据为空";
                    MessageBox.Show(msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Error); //出错
                    return;
                }

                if (status.status_submit_send == YesOrNo.Yes)
                {
                    continue; //已提交,就不用再次提交了,跳过
                }

                //API提交
                YesOrNo yn_submit = ApiCall.Do_SubmitSend(file, ref status); //提交一行API发送
                ShowStatus(lvi, status); //更新提交发送状态
                if (yn_submit != YesOrNo.Yes)
                {
                    continue; //提交失败
                }

                //如果提交发送成功则转移文件
                if (status.status_submit_send == YesOrNo.Yes)
                {
                    MoveSendedFile(file);
                }

                Application.DoEvents(); //放手
            } //foreach

            progressBar1.Value = (int) (((double) (p_index)/(double) p_count)*100.00d); //进度条
            Thread.Sleep(100);
        }

        /// <summary>
        /// 2.更新选中的发送状态
        /// </summary>
        /// <param name="scroll">是否滚动</param>
        private void Do_RenewSendStatus_Checked(bool scroll)
        {
            List<ListViewItem> list = GetCheckedItems();
            int p_count = list.Count;//总数
            int p_index = 0;//第几个
            //progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条
            foreach (ListViewItem lvi in list)
            {
                progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条
                lblProgress.Text = String.Format("{0}/{1}", p_index, p_count);//进度

                int idx = lvi.Index;
                lvi.Selected = true; //选中行,灰色
                if (scroll)
                {
                    LvFiless.EnsureVisible(idx); //确保当前选定项是可见的,必要时使控件内容滚动
                }

                ApiStatus status = lvi.Tag as ApiStatus;
                if (status == null)
                {
                    ZBug.Log("发送进度,没有内置状态对象错误,跳过");
                    continue;
                }
                if (status.status_submit_send != YesOrNo.Yes)
                {
                    ZBug.Log("没有提交发送过,无需获取进度");
                    continue;
                }
                if (status.status_progress == YesOrNo.Yes)
                {
                    ZBug.Log("发送进度已经完成了,无需再更新发送进度");
                    continue;
                }

                //string task_id = status.task_id;

                //API
                //bool success = ApiCallDemo.Do_GetSendProgress(ref status, out api_message);
                YesOrNo success = ApiCall.Do_GetSendProgress(ref status);//, out api_message
                //lvi.SubItems[SubItem_发送状态].Text = success.ToString();
                //lvi.SubItems[SubItem_备注].Text = api_message;
                ShowStatus(lvi, status);
                if (success == YesOrNo.Error)
                {
                    //访问出错
                    continue;
                }

                Application.DoEvents();//放手
            }//foreach

            progressBar1.Value = (int)(((double)(p_index) / (double)p_count) * 100.00d);//进度条
            Thread.Sleep(100);
        }

        /// <summary>
        /// 3.更新选中的阅读状态
        /// </summary>
        private void Do_RenewReadStatus_Checked()
        {
            List<ListViewItem> list = GetCheckedItems();
            int p_count = list.Count; //总数
            int p_index = 0; //第几个
            //progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条

            foreach (ListViewItem lvi in list)
            {
                progressBar1.Value = (int) (((double) (p_index++)/(double) p_count)*100.00d); //进度条
                lblProgress.Text = String.Format("{0}/{1}", p_index, p_count); //进度

                int idx = lvi.Index;
                lvi.Selected = true; //选中行,灰色
                LvFiless.EnsureVisible(idx); //确保当前选定项是可见的,必要时使控件内容滚动

                ApiStatus status = lvi.Tag as ApiStatus;
                if (status == null)
                {
                    LogFile.Instance().LogText("结果阅读状态,没有内置状态对象错误,跳过");
                    continue;
                }
                if (status.status_submit_send != YesOrNo.Yes)
                {
                    continue; //无提交发送
                }
                if (status.status_progress != YesOrNo.Yes)
                {
                    continue; //发送进度未完成
                }
                if (status.status_result_read == YesOrNo.Yes)
                {
                    continue; //已读了
                }
                if (status.status_recall == YesOrNo.Yes)
                {
                    continue; //已经撤回了
                }

                //string task_id = status.task_id;
                //string useridss = string.Format("{0}", status.user_idss); //发送用户清单

                //API
                //bool success = ApiCallDemo.Do_GetResultReadStatus(ref status, out api_message);
                YesOrNo readed_yesno = ApiCall.Do_GetResult_IsReaded(ref status);//, out api_message
                //lvi.SubItems[SubItem_阅读状态].Text = readed_yesno.ToString(); //是否已读状态
                //lvi.SubItems[SubItem_备注].Text = String.Format("{0}", api_message);
                ShowStatus(lvi, status);
                if (readed_yesno == YesOrNo.Error)
                {
                    continue; //访问出错,继续下一条
                }
                Application.DoEvents(); //放手
            } //foreach

            progressBar1.Value = (int) (((double) (p_index++)/(double) p_count)*100.00d); //进度条
            Thread.Sleep(100);
        }

        /// <summary>
        /// 4.撤回选中的消息
        /// </summary>
        private void Do_Recall_Checked()
        {
            List<ListViewItem> list = GetCheckedItems();
            int p_count = list.Count;//总数
            int p_index = 0;//第几个
            //progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条
            foreach (ListViewItem lvi in list)
            {
                progressBar1.Value = (int)(((double)(p_index++) / (double)p_count) * 100.00d);//进度条
                lblProgress.Text = String.Format("{0}/{1}", p_index, p_count);//进度

                int idx = lvi.Index;
                lvi.Selected = true; //选中行,灰色
                LvFiless.EnsureVisible(idx);//确保当前选定项是可见的,必要时使控件内容滚动

                ApiStatus status = lvi.Tag as ApiStatus;
                if (status == null)
                {
                    LogFile.Instance().LogText("没有内置状态对象错误,跳过撤回");
                    continue;
                }
                if (status.status_submit_send != YesOrNo.Yes)
                {
                    LogFile.Instance().LogText("没有提交发送过,无需撤回");
                    continue;
                }
                if (status.status_progress != YesOrNo.Yes)
                {
                    LogFile.Instance().LogText("未发送成功,无需撤回");
                    continue;
                }
                if (status.status_recall == YesOrNo.Yes)
                {
                    LogFile.Instance().LogText("已经撤回,无需再次撤回");
                    continue;
                }

                //string task_id = status.task_id;

                //API
                //bool success = ApiCallDemo.Do_Recall(ref status, out api_message);
                //bool success = ApiCall.Do_Recall(ref status, out api_message);
                YesOrNo yn = ApiCall.Do_Recall(ref status);//, out api_message
                ShowStatus(lvi, status);
                if (yn == YesOrNo.Error)
                {
                    //访问出错
                    //lvi.SubItems[SubItem_撤回].Text = yn.ToString();
                    //lvi.SubItems[SubItem_备注].Text = api_message;
                    continue;
                }

                //撤回状态
                //lvi.SubItems[SubItem_撤回].Text = yn.ToString();
                //lvi.SubItems[SubItem_备注].Text = String.Format("{0}", status.remark_text);

                Application.DoEvents();//放手
            }//foreach

            progressBar1.Value = (int)(((double)(p_index) / (double)p_count) * 100.00d);//进度条
            Thread.Sleep(100);
        }

        #endregion

        

    }
}
